package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfInwardRemittanceService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.database.mybatisplus.PageUtil;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.common.enums.trade.status.InwardMerRemitStatusEnum;
import com.mdb.open.common.enums.trade.status.InwardProdRemitStatusEnum;
import com.mdb.open.common.enums.trade.status.InwardRemitCreditStatusEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.GetInwardRemitBo;
import com.mdb.open.trade.domain.bo.trade.QueryInwardRemittancePageListBo;
import com.mdb.open.trade.domain.bo.trade.UpdateInwardRemitBo;
import com.mdb.open.trade.domain.po.trade.InwardRemittance;
import com.mdb.open.trade.mapper.trade.TfInwardRemittanceMapper;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  汇入表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfInwardRemittanceService extends SuperService<TfInwardRemittanceMapper, InwardRemittance> {

    public IPage<InwardRemittance> pageQuery(QueryInwardRemittancePageListBo bo) {


        LambdaQueryWrapper<InwardRemittance> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bo.getReceiveAcc())) {
            queryWrapper.eq(InwardRemittance::getReceiveAcc, bo.getReceiveAcc());
        }
        if (ObjectUtil.isNotEmpty(bo.getRemitAcc())) {
            queryWrapper.eq(InwardRemittance::getRemitAcc, bo.getRemitAcc());
        }
        if (ObjectUtil.isNotEmpty(bo.getStatus())) {
            queryWrapper.eq(InwardRemittance::getStatus, bo.getStatus().getStatusEnum());
        }
        if (ObjectUtil.isNotEmpty(bo.getStartDate()) && ObjectUtil.isNotEmpty(bo.getEndDate())) {
            queryWrapper.between(InwardRemittance::getRemitDate, bo.getStartDate(), bo.getEndDate());
        }
        queryWrapper.eq(InwardRemittance::getProdStatus, InwardProdRemitStatusEnum.NORMAL);
        queryWrapper.eq(InwardRemittance::getMerStatus, InwardMerRemitStatusEnum.NORMAL);
        queryWrapper.eq(InwardRemittance::getCreditStatus, InwardRemitCreditStatusEnum.ALREADY_ACCOUNTED);
        queryWrapper.eq(InwardRemittance::getCustomerCode, bo.getCustomerCode());


        return this.baseMapper.selectPage(PageUtil.initPage(bo), queryWrapper);
    }


    public ResultEx<InwardRemittance> getInwardRemitByNo(GetInwardRemitBo bo) {


        LambdaQueryWrapper<InwardRemittance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InwardRemittance::getRemitNo, bo.getRemitNo());
        queryWrapper.eq(InwardRemittance::getCustomerCode, bo.getCustomerCode());
        if (ObjectUtil.isNotEmpty(bo.getStatus())) {
            queryWrapper.eq(InwardRemittance::getStatus, bo.getStatus());
        }
        InwardRemittance remittance = this.baseMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(remittance)) {
            return ResultEx.fail();
        }

        return ResultEx.ok(remittance);

    }

    public ResultEx updateInwardRemit(UpdateInwardRemitBo bo) {

        InwardRemittance updateRemittance = new InwardRemittance();

        LambdaUpdateWrapper<InwardRemittance> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(InwardRemittance::getCustomerCode, bo.getCustomerCode());
        updateWrapper.eq(InwardRemittance::getRemitNo, bo.getRemitNo());
        if (ObjectUtil.isNotEmpty(bo.getStatus())) {
            updateRemittance.setStatus(bo.getStatus());
            updateWrapper.eq(InwardRemittance::getStatus, bo.getOldStatus());
        }
        if (ObjectUtil.isNotEmpty(bo.getMerStatus())) {
            updateRemittance.setMerStatus(bo.getMerStatus());
            updateWrapper.eq(InwardRemittance::getMerStatus, bo.getOldMerStatus());
        }
        if (ObjectUtil.isNotEmpty(bo.getProdStatus())) {
            updateRemittance.setProdStatus(bo.getProdStatus());
            updateWrapper.eq(InwardRemittance::getProdStatus, bo.getOldProdStatus());
        }
        if (ObjectUtil.isNotEmpty(bo.getCreditStatus())) {
            updateRemittance.setCreditStatus(bo.getCreditStatus());
            updateWrapper.eq(InwardRemittance::getCreditStatus, bo.getOldCreditStatus());
        }

        int count = this.baseMapper.update(updateRemittance, updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok();
    }

}
